import modelAbstract from "./modelAbstract";
import { images } from "../service/image";
import bullet from "../canvas/bullet";
import config from "../config";
import { directionEnum } from '../enum/directionEnum';
import untils from "../untils";
import steel from "../canvas/steel";
import wall from "../canvas/wall";
import tank from "../canvas/tank";
import play from "../canvas/play";
import boss from "../canvas/boss";
import { conforms } from "lodash";
// 草地模型子类
export default class bulletModel extends modelAbstract implements bulletIModel{
  public ctx: ICanvas = bullet;
  name: string = 'bullet';
  constructor(public tank: IModel) {
    super(tank.posX + config.model.width / 2, tank.posY + config.model.height / 2);
    this.direction = tank.direction as directionEnum;
  }
  render(): void {
    let x = this.posX;
    let y = this.posY;
    let step = this.tank.name === 'play' ? 8 : 5;
    switch (this.direction) {
      case directionEnum.top:
        y -= step;
        break;
      case directionEnum.bottom:
        y += step;
        break;
      case directionEnum.right:
        x += step;
        break;
      case directionEnum.left:
        x -= step;
        break;
    };
    // 碰撞检测
    let touchModel = untils.isModelTouch(x, y, config.model.bulletSize, config.model.bulletSize, [...steel.modelist, ...wall.modelist, ...tank.modelist, ...play.modelist, ... boss.modelist]);
    if (untils.isCanvasTouch(x, y, config.model.bulletSize, config.model.bulletSize)) {
      this.destroy();
    } else if (touchModel && touchModel.name !== this.tank.name) {
      this.destroy();
      this.blast(touchModel);
      (touchModel.name != "steel") && touchModel.destroy();
    } else {
      this.posX = x;
      this.posY = y;
      this.draw();
    };
  };
  image() {
    return images.get("bullet")!;
  };
  protected draw() {
    this.ctx.canvas.drawImage(this.image(), this.posX, this.posY, config.model.bulletSize, config.model.bulletSize);
  };
};